# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def maxPathSum(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        self.maxs = -999999
        self.help(root)
        return self.maxs
    def help(self, node):
        if not node:
            return 0
        left = self.help(node.left)
        right = self.help(node.right)
        #        算父节点
        curf = max(max(left, right) + node.val, node.val)
        #        不算父节点
        cur = max(left + right + node.val, curf)
        self.maxs = max(cur, self.maxs)
        return curf
